const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

// Generate pages object
const pagesObj = {};

const chromeName = ["popup", "options"];

chromeName.forEach(name => {
    pagesObj[name] = {
        entry: `src/${name}/index.js`,
        template: "public/index.html",
        filename: `${name}.html`
    };
});

const plugins =
    process.env.NODE_ENV === "production"
        ? {
            patterns: [
                {
                    from: path.resolve("src/manifest.production.json"),
                    to: `${path.resolve("dist")}/manifest.json`
                },
                {
                    from: path.resolve("src/background/background.js"),
                    to: `${path.resolve("dist")}/js/background.js`
                },
                {
                    from: path.resolve("src/background/background.html"),
                    to: `${path.resolve("dist")}/background.html`
                },
                {
                    from: path.resolve("src/background/Eluveitie - Isara.mp3"),
                    to: `${path.resolve("dist")}/Eluveitie - Isara.mp3`
                }
            ]
        }
        : {
            patterns: [
                {
                    from: path.resolve("src/manifest.development.json"),
                    to: `${path.resolve("dist")}/manifest.json`
                },
                {
                    from: path.resolve("src/background/background.js"),
                    to: `${path.resolve("dist")}/js/background.js`
                },
                {
                    from: path.resolve("src/background/background.html"),
                    to: `${path.resolve("dist")}/background.html`
                },
                {
                    from: path.resolve("src/background/Eluveitie - Isara.mp3"),
                    to: `${path.resolve("dist")}/Eluveitie - Isara.mp3`
                }
            ]
        };

module.exports = {
    pages: pagesObj,
    // 去掉文件名中的 hash
    filenameHashing: false,
    lintOnSave: false,
    configureWebpack: {
        plugins: [new CopyWebpackPlugin(plugins)]
    },

    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: []
        }
    },
    css: {
        loaderOptions: {
            less: {
                lessOptions: {
                    javascriptEnabled: true
                }
            }
        }
    }
};
